package org.cfx.org.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.cfx.org.domain.Department;
import org.cfx.org.domain.Employee;
import org.cfx.org.query.DepartmentQuery;
import org.cfx.org.query.EmployeeQuery;
import org.cfx.org.service.IDepartmentService;
import org.cfx.org.service.IEmployeeService;
import org.cfx.org.utils.AjaxResult;
import org.cfx.org.utils.PageInfo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RTX 9090
 *
 *
 * RestFul:请求风格
 *      使用http请求类型替换请求路径里面操作的动词，安全性高
 *      @RequestMapping 可以接收多种请求类型 method = RequestMethod.PUT可以接收单一的请求类型
 *      使用RestFul后不会再使用@RequestMapping
 *          @PutMapping-->接收put请求类型
 *          @DeleteMapping
 */
@RestController
@RequestMapping("/employee")
@Tag(name = "用户管理",description = "用户的CRUD")
public class EmployeeController {
    @Resource
    private IDepartmentService departmentService;

    @Resource
    private IEmployeeService employeeService;

//    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.PUT)
    @PutMapping
    @Operation(summary = "添加或者修改",description = "需要填写用户信息")
    public AjaxResult saveOrUpdate(@Parameter(description = "用户信息")
                                       @RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage("Error saving employee");
        }
    }

//    @RequestMapping("/delete/{id}")
    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除用户")
    public AjaxResult delete(@Parameter(description = "填用户id就行了")
                                 @PathVariable("id") Long id) {

        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage("Error");
        }
    }

    @DeleteMapping("/deleteList")
    @Operation(summary = "批量删除用户")
    public AjaxResult deleteList() {
        return new AjaxResult().me().setSuccess(false);
    }

//    @RequestMapping("/query/{id}")

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询用户")
    public AjaxResult queryById(@Parameter(description = "填需要查询的用户id就行了")
                                    @PathVariable("id") Long id) {
        return new AjaxResult().setData(employeeService.queryById(id));
    }


    @GetMapping
    @Operation(summary = "查询所有用户")
    public AjaxResult queryAll() {
        List<Employee> employees = employeeService.queryAll();
        return new AjaxResult().setData(employees);
    }

    @PostMapping
    @Operation(summary = "分页+查询所有用户")
    public AjaxResult queryPage(@RequestBody EmployeeQuery employeeQuery) {
        PageInfo<Employee> employeePageInfo = employeeService.queryAllPage(employeeQuery);
        return new AjaxResult().setData(employeePageInfo);
    }

    @GetMapping("/manager")
    @Operation(summary = "查询所有经理")
    public AjaxResult queryManager() {
        List<Employee> employees = employeeService.queryManager();
        return new AjaxResult().setData(employees);
    }
}
